/*
 * test_rapidjson.h
 *
 *  Created on: 2014. 7. 2.
 *      Author: jjaehuny
 */

#ifndef TEST_RAPIDJSON_H_
#define TEST_RAPIDJSON_H_

#include <gtest/gtest.h>
#include <glog/logging.h>

#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>

class test_rapidjson : public testing::Test
{
public:
	class Dummy
	{
	public:
		Dummy() {}
		~Dummy() {}
	};

public:
	// Sets up the test fixture.
	virtual void SetUp() {}

	// Tears down the test fixture.
	virtual void TearDown() {}
};

TEST_F(test_rapidjson, test_rapidjson)
{
	const char json[] = "{ \"hello\" : \"world\" }";

	rapidjson::Document d;
	d.Parse<0>(json);

	LOG(INFO) << "[hello : " << d["hello"].GetString() << "]";

	rapidjson::Document jsonDoc;
	jsonDoc.SetObject();
	rapidjson::Document::AllocatorType& allocator = jsonDoc.GetAllocator();
	jsonDoc.AddMember("name", "jjaehuny", allocator);
	jsonDoc.AddMember("1", "1", allocator);
	jsonDoc.AddMember("2", "2", allocator);

	rapidjson::Value myArray(rapidjson::kArrayType);
	{
	    rapidjson::Value objValue;
	    objValue.SetObject();
	    objValue.AddMember("playername", "aa", allocator);
	    objValue.AddMember("position", "bb", allocator);

	    myArray.PushBack(objValue, allocator);
	}
	jsonDoc.AddMember("array", myArray, allocator);

	rapidjson::StringBuffer strbuf;
	rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
	jsonDoc.Accept(writer);

	std::string str = strbuf.GetString();
	LOG(INFO) << "[str : " << str << "]";

	rapidjson::Document reader;
	reader.Parse<0>(str.c_str());

	const rapidjson::Value & array = reader["array"];
	for (rapidjson::SizeType i = 0; i < array.Size(); i++)
	{
		//rapidjson::Document var = array[i];
		//LOG(INFO) << var["playername"];
		const rapidjson::Value & var = array[i];

		LOG(INFO) << "[playername : " << var["playername"].GetString() << "]";
		LOG(INFO) << "[position : " << var["position"].GetString() << "]";
		//LOG(INFO) << array[i]["position"].GetString();
	}
}


#endif /* TEST_RAPIDJSON_H_ */
